rm(list= ls())

library(tidyverse)
library(sjlabelled)

## Load Forsa 2015 and clean 
## Data is available here: https://search.gesis.org/research_data/ZA6280 

forsa15 <- read_stata('data_private/ZA6280_v1-0-0.dta') %>%
  dplyr::select(one_of('bland_','regbez_', 'lkreis_', 'ges', 
                       'sbw95l', 'dat_day', 'mon')) %>% 
  filter(ges != 3) %>% ## exclude gender NAs 
  mutate(bland_ = as.character(bland_)) %>% 
  filter(!is.na(ges)) %>% 
  mutate(bland_ = ifelse(nchar(bland_) == 1, paste0('0', bland_), bland_),
         lkreis_ = ifelse(nchar(lkreis_) == 1, paste0('0', lkreis_), lkreis_),
         county_id = paste0(bland_, regbez_, lkreis_, sep = ''),
         male = ifelse(ges == 1, 1, 0),
         year = 2015) %>%
  dplyr::select(-one_of('regbez_', 'lkreis_', 'ges')) %>%
  rename('btw_vote' = 'sbw95l') %>%
  mutate(btw_vote = as.character(btw_vote)) %>%
  mutate(idate = as.Date(paste(year, mon, dat_day, sep = '-'))) %>%
  dplyr::select(-one_of('dat_day', 'mon')) %>% 
  dplyr::select(idate, year, male, everything()) %>%
  mutate(year = as.character(year)) %>%
  rename(bland = bland_)

## Recode BTW Vote Choice Variable for 2015 survey 

forsa15 <- forsa15 %>%
  mutate(btw_vote = recode(as.character(btw_vote),
                           `1` = 'CDU/CSU',
                           `2`= 'CDU/CSU',
                           `3` = 'SPD', 
                           `4` = 'FDP',
                           `5` = 'Greens',
                           `6` = 'Linke', 
                           `7` = 'other',
                           `8` = 'other',
                           `9` = 'other',
                           `10` = 'other',
                           `11` = 'AFD',
                           `95` = 'not eligible',
                           `97` = 'non-voter',
                           `96` = 'other', 
                           `98` = 'dont know',
                           `99` = 'NA')) %>%
  filter(!is.na(bland))

## Load and clean Forsa 2016 
## Data is available here: https://search.gesis.org/research_data/ZA6704 

forsa16 <-  read_stata('data_private/ZA6704_v1-0-0.dta')  %>% dplyr::select(all_of(c('datum', 
                                                                                     'ges', 
                                                                                     'sbw98',
                                                                                     'bland',
                                                                                     'lkreis',
                                                                                     'regbez'))) %>%
  filter(ges != 3) %>% ## exclude gender NAs 
  filter(!is.na(ges)) %>% 
  mutate(idate = ymd(datum), 
         year = year(idate),
         year = as.character(year),
         male = ifelse(ges == 1, 1, 0)) %>%
  dplyr::select(-one_of('datum', 'ges')) %>% 
  dplyr::select(idate, year, male, everything()) %>%
  rename('btw_vote' = 'sbw98')  

## Get county ID 

forsa16 <- forsa16 %>% 
  mutate(bland = as.character(bland)) %>% 
  filter(!is.na(lkreis)) %>% 
  mutate(bland = ifelse(nchar(bland) == 1, paste0('0', bland), bland),
         lkreis = ifelse(nchar(lkreis) == 1, paste0('0', lkreis), lkreis),
         county_id = paste0(bland, regbez, lkreis, sep = ''))

## Recode vote choice 

forsa16$btw_vote <- recode(as.character(forsa16$btw_vote),
                           `1` = 'CDU/CSU',
                           `2`= 'CDU/CSU',
                           `3` = 'SPD', 
                           `4` = 'FDP',
                           `5` = 'Greens',
                           `6` = 'Linke', 
                           `7` = 'other',
                           `8` = 'AFD',
                           `9` = 'other',
                           `10` = 'other',
                           `11` = 'other',
                           `12` = 'not eligible',
                           `13` = 'non-voter',
                           `14` = 'other', 
                           `15` = 'dont know',
                           `16` = 'NA')

## Merge 2015 + 2016 Data 

forsa <- bind_rows(forsa15, forsa16)

## Binary vote choice variables 

forsa <- forsa %>% 
  mutate(vote_cdu_csu = ifelse(btw_vote == 'CDU/CSU', 1, 0),
         vote_afd = ifelse(btw_vote == 'AFD', 1, 0),
         vote_spd = ifelse(btw_vote == 'SPD', 1, 0),
         vote_left = ifelse(btw_vote == 'Linke', 1, 0),
         vote_greens = ifelse(btw_vote == 'Greens', 1, 0),
         vote_fdp = ifelse(btw_vote == 'FDP', 1, 0),
         vote_nonvoter = ifelse(btw_vote == 'non-voter', 1, 0))

## Data Analysis 

dat <- forsa %>%
  mutate(year_month = paste(year, lubridate::month(idate), sep = '_')) %>%
  filter(bland %in% c('05', '09')) %>%
  filter(!is.na(vote_cdu_csu)) %>%
  filter(idate <= as.Date('2016-08-06')) 

treat <- readRDS('data/treated_by_county.rds') 
treat$state <- NULL 

dat <- dat %>%
  left_join(treat, by = c('county_id' = 'county.id')) %>%
  mutate(treat_label = ifelse(treated == 1, 'Labor market liberalized\n(treated)', 
                              'Labor market restricted\n(control)'))  %>%
  filter(!is.na(treated))

## Combined right vote 

dat <- dat %>%
  mutate(vote_right = ifelse(vote_afd == 1 | vote_cdu_csu == 1, 1, 0),
         vote_left_sum = ifelse(vote_spd == 1 | vote_greens == 1 | vote_left == 1, 1, 0))


## Figure A4

p_right <- ggplot(dat, aes(x = idate, y = vote_right, 
                           group = treat_label,
                           col = treat_label)) + 
  geom_smooth(method = 'lm', se = T) + 
  stat_summary_bin(dat = dat, 
                   fun = mean, geom = "point", binwidth = 10) + 
  theme_bw() +
  geom_vline(xintercept=as.numeric(as.Date('2016-08-06')), 
             linetype = 'dotdash') +
  labs(col = '',
       y = 'Vote intention:\nCDU/CSU/AfD',
       x = '') + 
  scale_color_manual(values = c('darkred', 'black'))  +
  scale_x_date(limits = as.Date(c("2015-01-01", "2016-08-30")),
               date_breaks = "1 month",
               date_labels = "%b%Y") + 
  theme(legend.position = 'bottom') 

p_right

## Figure A5

p_left <- ggplot(dat, aes(x = idate, y = vote_left_sum, 
                          group = treat_label,
                          col = treat_label)) + 
  geom_smooth(method = 'lm', se = T) + 
  stat_summary_bin(dat = dat, 
                   fun = mean, geom = "point", binwidth = 10) + 
  theme_bw() +
  geom_vline(xintercept=as.numeric(as.Date('2016-08-06')), 
             linetype = 'dotdash') +
  labs(col = '',
       y = 'Vote intention:\nSPD/Greens/Left Party',
       x = '') + 
  scale_color_manual(values = c('darkred', 'black'))  +
  scale_x_date(limits = as.Date(c("2015-01-01", "2016-08-30")),
               date_breaks = "1 month",
               date_labels = "%b%Y") + 
  theme(legend.position = 'bottom') 


p_left

